package com.yanqu.road.dao.impl.activity.diggold;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.diggold.data.DigGoldUserData;
import com.yanqu.road.entity.activity.diggold.entity.DigGoldBlockData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DigGoldUserDaoImpl extends TempDataObjectDao<DigGoldUserData> {
    public static String getDigGoldUserDataSql = "select * from `t_u_dig_gold_user_data` where `activityId`=? and `userId`=?";
    public DigGoldUserData getDigGoldUserData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getDigGoldUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateDigGoldUserDataSql = "update `t_u_dig_gold_user_data` set inCol=?,blockDataList=?,boxList=?,mapIndex=?,antiqueNumMap=?,score=?,antiqueScore=?,noPalaceTime=?,stratumIndexMap=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(DigGoldUserData digGoldUserData) {
        digGoldUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, digGoldUserData.getInCol()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(digGoldUserData.getBlockDataList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(digGoldUserData.getBoxList())));
        para.put(i++, new DbParameter(Types.INTEGER, digGoldUserData.getMapIndex()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(digGoldUserData.getAntiqueNumMap())));
        para.put(i++, new DbParameter(Types.BIGINT, digGoldUserData.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, digGoldUserData.getAntiqueScore()));
        para.put(i++, new DbParameter(Types.INTEGER, digGoldUserData.getNoPalaceTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(digGoldUserData.getStratumIndexMap())));
        para.put(i++, new DbParameter(Types.INTEGER, digGoldUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, digGoldUserData.getUserId()));
        if (getDb().executeNoneQuery(updateDigGoldUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertDigGoldUserDataSql = "insert into `t_u_dig_gold_user_data`(activityId,userId,inCol,blockDataList,boxList,mapIndex,antiqueNumMap,score,antiqueScore,noPalaceTime,stratumIndexMap) values (?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(DigGoldUserData digGoldUserData) {
        digGoldUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, digGoldUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, digGoldUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, digGoldUserData.getInCol()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(digGoldUserData.getBlockDataList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(digGoldUserData.getBoxList())));
        para.put(i++, new DbParameter(Types.INTEGER, digGoldUserData.getMapIndex()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(digGoldUserData.getAntiqueNumMap())));
        para.put(i++, new DbParameter(Types.BIGINT, digGoldUserData.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, digGoldUserData.getAntiqueScore()));
        para.put(i++, new DbParameter(Types.INTEGER, digGoldUserData.getNoPalaceTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(digGoldUserData.getStratumIndexMap())));
        if (getDb().executeNoneQuery(insertDigGoldUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected DigGoldUserData parseData(ResultSet rs) throws SQLException {
        DigGoldUserData digGoldUserData = new DigGoldUserData();
        digGoldUserData.setActivityId(rs.getInt("activityId"));
        digGoldUserData.setUserId(rs.getLong("userId"));
        digGoldUserData.setInCol(rs.getInt("inCol"));
        digGoldUserData.setBlockDataList(JSON.parseObject(rs.getString("blockDataList"), new TypeReference<java.util.List<com.yanqu.road.entity.activity.diggold.entity.DigGoldBlockData>>() {}));
        digGoldUserData.setBoxList(JSON.parseObject(rs.getString("boxList"), new TypeReference<ConcurrentHashMap<java.lang.Integer, java.util.List<java.lang.Integer>>>() {}));
        if (digGoldUserData.getBoxList() == null) {
            digGoldUserData.setBoxList(new ConcurrentHashMap<>());
        }
        digGoldUserData.setMapIndex(rs.getInt("mapIndex"));
        digGoldUserData.setAntiqueNumMap(JSON.parseObject(rs.getString("antiqueNumMap"), new TypeReference<ConcurrentHashMap<java.lang.Integer, java.lang.Integer>>() {}));
        if (digGoldUserData.getAntiqueNumMap() == null) {
            digGoldUserData.setAntiqueNumMap(new ConcurrentHashMap<>());
        }
        digGoldUserData.setScore(rs.getLong("score"));
        digGoldUserData.setAntiqueScore(rs.getLong("antiqueScore"));
        digGoldUserData.setNoPalaceTime(rs.getInt("noPalaceTime"));
        digGoldUserData.setStratumIndexMap(JSON.parseObject(rs.getString("stratumIndexMap"), new TypeReference<ConcurrentHashMap<java.lang.Integer, java.lang.Integer>>() {}));
        if (digGoldUserData.getStratumIndexMap() == null) {
            digGoldUserData.setStratumIndexMap(new ConcurrentHashMap<>());
        }
        return digGoldUserData;
    }

}
